﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOAModels;
using TarreOA.TarreOABLL;

public partial class admin_UserurlManager : System.Web.UI.Page
{
    static List<UserURL> list = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Message.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 15)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            txtURL.Text = @"http://";
            TarreOA.TarreOAModels.User user=(User)Session["user"];
            list = UserURLManager.FindByUserId(user.UserId);
            GridView1.DataSource = list;
            if (Request.QueryString["pageIndex"]!=null && Request.QueryString["pageIndex"]!="")
            {
                GridView1.PageIndex = Convert.ToInt32(Request.QueryString["pageIndex"]);
            }
            GridView1.DataBind();
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        UserURL userurl = new UserURL();
        userurl.Url = txtURL.Text;
        userurl.Remark = txtRemark.Text;
        userurl.User=(User)Session["user"];
        if (UserURLManager.AddUserURL(userurl))
        {
            TarreOA.TarreOAModels.User user = (User)Session["user"];
            list = UserURLManager.FindByUserId(user.UserId);
            GridView1.DataSource = list;
            GridView1.DataBind();
            ClientScript.RegisterStartupScript(this.GetType(),"","alert('添加成功！')",true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加失败！请重试！')", true);
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = list;
        GridView1.DataBind();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "updateURL")
        {
            Response.Redirect("UpdateUserUrl.aspx?id="+e.CommandArgument.ToString()+"&pageIndex="+GridView1.PageIndex.ToString());
        }
        else if (e.CommandName == "delURL")
        {
            if (UserURLManager.DelUserURLById(Convert.ToInt32(e.CommandArgument)))
            {
                int pageIndex = GridView1.PageIndex;
                TarreOA.TarreOAModels.User user = (User)Session["user"];
                list = UserURLManager.FindByUserId(user.UserId);
                GridView1.DataSource = list;
                GridView1.PageIndex = pageIndex;
                GridView1.DataBind();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败！请重试！')", true);
            }
        }
    }
    protected void btnDelAll_Click(object sender, EventArgs e)
    {
        TarreOA.TarreOAModels.User user = (User)Session["user"];
        if (UserURLManager.DelUserURLByUserId(user.UserId))
        {
            list = UserURLManager.FindByUserId(user.UserId);
            GridView1.DataSource = list;
            GridView1.DataBind();
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除失败！请重试！')", true);
        }
    }
}
